#include "thumbnails.h"
#include <poppler-qt4.h>
#include <iostream>
//****************************************************************************************************************
// Filename: thumbnails.cpp
// implemetation for ThumnailsDoc class
// data March 2012
//****************************************************************************************************************

ThumbnailsDock::ThumbnailsDock(QWidget *parent):QListWidget(parent)
{
   //set height 
   thumbHeight = 100;
   connect(this,SIGNAL(itemClicked(QListWidgetItem * )),this,SLOT( pageClickHandler(QListWidgetItem*)));

}
ThumbnailsDock::~ThumbnailsDock()
{
   
}

// function to return mouse move dsitance
double ThumbnailsDock::getDistance()
{
   return travelDistance;
}

// function to track mouse move distance
void ThumbnailsDock::mouseMoveEvent(QMouseEvent * event)
{
   // track mouse move distance
   if(startTest)
   {
      if(last == QPointF(-1,-1))
	 last = event->globalPos();
      else
      {
	 QPointF current = event->globalPos();
	 QLineF line(last,current);
	 travelDistance += line.length();
	 last = current;
      }
   }
}

//set move filter for each child widget 
bool  ThumbnailsDock::eventFilter(QObject *obj, QEvent *event)
{
   if(startTest && event->type() == QEvent::MouseMove)
   {
      QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
      if(last == QPointF(-1,-1))
	 last = mouseEvent->globalPos();
      else
      {
	 QPointF current = mouseEvent->globalPos();
	 QLineF line(last,current);
	 travelDistance += line.length();
	 last = current;
      }
      return true;
   }
   else
      return QObject::eventFilter(obj, event);
}


//function to start collect data
void  ThumbnailsDock::beginTest()
{
   startTest = true;
   travelDistance = 0.0;
   last.setX(-1);
   last.setY(-1);
}

// function to abort testing
void  ThumbnailsDock::abortTest()
{
   startTest = false;
}
// handle a page click singal
// send out poage number
void ThumbnailsDock::pageClickHandler(QListWidgetItem * item)
{
   emit pageClicked(row(item)+1);
}

//function to init doc
void ThumbnailsDock::initDoc(QVector<QImage> p)
{
  // clear current list if any
   clear();
   // compute thumb size
   if(p.size())
   {
      double ratio = (double)p[0].width() /(double)p[0].height();
      thumbWidth = (int)((double)thumbHeight * ratio);
   }
   
   // put thumb into list widegt
   for(int i = 0 ; i < p.size() ; ++i)
   {
      
      QImage image = p[i].scaled(thumbWidth,thumbHeight ,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
      QSize size(image.size());
      size.setHeight(size.height()+2);
      QLabel * current = new QLabel(this);
      current->setAlignment(Qt::AlignCenter);
      current->setPixmap(QPixmap::fromImage(image));

      current->setMouseTracking(true);
      current->installEventFilter(this);
      
      QListWidgetItem * WidgetItem = new QListWidgetItem;
      WidgetItem->setBackground(QBrush(QColor(127, 127, 127, 255)));
      WidgetItem -> setSizeHint(size);
      addItem(WidgetItem);
      setItemWidget(WidgetItem,current);
   }
   if(p.size() > 0)
      setCurrentRow(0);
  updateGeometry();;
}


